package Neko::Web::Controller::Root;

use strict;
use warnings;
use base 'Catalyst::Controller';
use LWP::UserAgent;
use Encode;

#
# Sets the actions in this controller to be registered with no prefix
# so they function identically to actions created in MyApp.pm
#
__PACKAGE__->config->{namespace} = '';

sub default : Private {
    my ( $self, $c ) = @_;

}

sub zip01 : Local {
    my ( $s , $c ) = @_;

    $c->stash->{template} = 'zip01.tt';

    if( $c->req->method eq 'POST' ) {
        $c->detach('zip01_do');
    }

}
sub zip01_do : Private {
    my ( $s ,$c ) = @_;

    
    $c->form({
        required => [qw/post_code/],
    });

    if( $c->has_dfv_error ) {
        return ;
    }

    $c->stash->{template} = 'zip01_do.tt';

}

sub zip02 : Local {
    my ( $s , $c ) = @_;

    $c->stash->{template} = 'zip02.tt';

    if( $c->req->method eq 'POST' ) {
        $c->detach('zip02_do');
    }
}

sub zip02_do : Private {
    my ($s ,$c ) = @_;

    $c->form({
        required => [qw/post_code/],
    });

    if( $c->has_dfv_error ) {
        return ;
    }

    my $v = $c->form->{valid};

    $v->{post_code} =~ s/-//g;

    my $url = 'http://zip.cgis.biz/csv/zip.php?zn=' . $v->{post_code};
    my $ua = LWP::UserAgent->new();
    my $res = $ua->get( $url );

    if( !$res->is_success) {
        $c->dfv_push_invalid('api_not_available');
        return;
    }

    my $csv = $res->content ; 
    my ( $a , $line ) = split( "\n", $csv );

    if( !$line ) {
        $c->dfv_push_invalid('not_found');
        return;
    }

    $c->stash->{line} = Encode::decode('euc-jp', $line ) ;
    Encode::_utf8_off( $c->stash->{line} );

    $c->stash->{template} = 'zip02_do.tt';

}

sub email01 : Local {
    my ( $s, $c ) =  @_;

    if( $c->req->method eq 'POST'){
        $c->detach('email01_do');
    }
}
sub email01_do : Private {
    my ( $s, $c ) = @_;

    $c->form({
        required => [qw/email/],
    });

    if( $c->has_dfv_error ) {
        return; 
    }

    $c->stash->{template} = 'email01_do.tt';

}

sub addition01 : Local {
    my ( $s, $c ) = @_;
    $c->stash->{template} = 'addition01.tt';

    if( $c->req->method eq 'POST' ) {
        $c->detach('addition01_do');
    }

}

sub addition01_do : Private {
    my ( $s, $c ) = @_;

    $c->form({
        required => [qw/num01 num02/],
    });

    if( $c->has_dfv_error ) {
        return ;
    }
    
    $c->stash->{template} = 'addition01_do.tt';

}

sub multiplication : Local {
    my ( $s ,$c ) = @_;
    $c->stash->{multiplication} = 'multiplication.tt';

    if( $c->req->method eq 'POST' ) {
        $c->detach('multiplication_do');
    }
}
sub multiplication_do : Private {
    my ( $s , $c ) = @_;

    $c->form({
        required => [qw/a_num b_num/],
    });

    if( $c->has_dfv_error ) {
        return;
    }
    
    $c->stash->{template} = 'multiplication_do.tt';
}

sub neko : Local {
    my ( $s ,$c ) = @_;
    $c->stash->{template} = 'neko.tt';

    if( $c->req->method eq 'POST' ) {
        $c->detach('neko_confirm');
    }
}

sub neko_confirm : Private {
    my ( $s, $c )  = @_;

    $c->form({required => [qw/neko/]});

    if( $c->has_dfv_error ) {
        return ;
    }

    my $v = $c->form->{valid};
    $c->session->{v} = $v;
    $c->stash->{template} = 'neko_confirm.tt';

}

sub neko_do : Local {
    my ( $s, $c ) = @_;

    $c->form({
        custom_parameters => $c->session->{v} || {} ,
        required          => [qw/neko/]
    });

    if( $c->has_dfv_error ) {
        $c->res->status('400');
        $c->res->body('error');
        return ;
    }

    $c->stash->{template} = 'neko_do.tt';
}

sub end : ActionClass('RenderView') {}

1;

=head1 NAME

Neko::Web::Controller::Root - Root Controller for Neko::Web

=head1 DESCRIPTION

[enter your description here]

=head1 METHODS

=cut

=head2 default

=cut


=head2 end

Attempt to render a view, if needed.


=head1 AUTHOR

Tomohiro Teranishi

=head1 LICENSE

This library is free software, you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut

